<?php
namespace app\user\controller;
use think\Input;
class Channel extends Common{
    public function initialize(){
        parent::initialize(); 
        $this->assign('shoplist',cache('shoplist'));   
        $this->assign('cionlist',cache('cionlist'));  
    }
    public function index(){
    	if(request()->isPost()){
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize'); 
            $userid=input('userid')?input('userid'):session('user.id');
            $list=db('channel')->where('userid',$userid)->order('open desc,id desc') 
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);
                $list['data'][$k]['numberall']=db('channel_order')->where('channelid',$v['id'])->sum('number').'/个';
                if($v['open']===0){
                	$list['data'][$k]['opentype'] ='<span class="red">审核中</span>';
                }else{
                	$list['data'][$k]['opentype'] ='<span class="gre">正常</span>';
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }

        $this->assign('title','通道管理');
        return $this->fetch();
    } 
    public function channelmin(){
        $this->assign('userid',input('userid'));  
        $this->assign('username',input('username'));  
        $this->assign('mobile',input('mobile'));  
        return $this->fetch();
    } 
    public function addchannel(){
    	if(request()->isPost()) {
    		$data = input('post.');  
            $data['addtime'] = time();   
            $shopappid=db('shop')->where('id',$data['shopid'])->find();
            $data['shopappid']=$shopappid['appid']; 
            $data['userid'] = $data['userid']?$data['userid']:session('user.id'); 
            $userss=db('users')->where('id',$data['userid'])->find();
            if($userss['is_lock']==1) return array('code'=>0,'msg'=>'该码商尚未来激活');  
            $data['agentid'] = $userss['agentid'];  
            $shoplv=explode('|',$shopappid['lv']);
            $agentlv=explode('|',$userss['agentlv']);
            $channellv=explode('|',$data['rate']);             
            if($channellv[0]>=($shoplv[0]-$agentlv[0]) ) return $result = ['code'=>-1,'msg'=>'【微信】手续费过高'];
            if($channellv[1]>=($shoplv[1]-$agentlv[1]) ) return $result = ['code'=>-1,'msg'=>'【支付宝】手续费过高'];
            if($channellv[2]>=($shoplv[2]-$agentlv[2]) ) return $result = ['code'=>-1,'msg'=>'【银行卡】手续费过高']; 
            $userbank=db('user_banks')->where('userid',$data['userid'])->where('open',1)->find();            
            if($userbank==false) return array('code'=>0,'msg'=>'该码商尚未设置收款账户'); 
            $cionprice =db('shop_cion')->where('id',$data['cionid'])->value('price');
            $data['price']=$cionprice;  
            if($data['userid']){ 
                $data['appid'] = $userss['appid']; 
                $data['appkey'] = $userss['appkey']; 
            }else{   
                $data['appid'] = $this->userInfo['appid']; 
                $data['appkey'] = $this->userInfo['appkey'];  
            } 
            $data['open'] = 0;  
            $id = db('channel')->insertGetId($data);
            if($id){ 
                return array('code'=>1,'msg'=>'开通成功,等待审核！');
            }else{
            	return array('code'=>-1,'msg'=>'开通失败'); 
            }
    	}else{
            $thisuserid=input('userid')?input('userid'):session('user.id'); 
            $this->assign('thisuserid',$thisuserid);
            $this->assign('userlv',input('userlv'));
	        $this->assign('title','新增通道');
	        return $this->fetch();
	    }
    } 
    //设置状态
    public function channelState(){  
        return ['status'=>-1,'msg'=>'请联系管理员开通!'];
        // if(db('channel')->where(array('id'=>input('id')))->update(array('open'=>input('open')))!==false){
        //     return ['status'=>1,'msg'=>'设置成功!'];
        // }else{
        //     return ['status'=>0,'msg'=>'设置失败!'];
        // }
    }
    
    public function getCion(){
        $shop=db('shop')->where('id',input("shopid"))->value('cions');
        $map[] =array('id','in',$shop);
        $list=db('shop_cion')->where($map)->select();
        return $list;
    } 
}